

import 'package:xim/http/apis.dart';
import 'package:xim/http/apis/message/message_model.dart';
import 'package:xim/http/http.dart';

class SessionModel {

  /// 会话Id
  final int id;

  /// 会话名称
  final String name;

  /// 会话创建者账号
  final String creatorAccount;

  /// 会话创建时间
  final DateTime createTime;

  /// 会话信息最后更新时间
  final DateTime? updateTime;

  /// 会话头像地址
  final String avatar;

  SessionModel({
    required this.id,
    required this.name,
    required this.creatorAccount,
    required this.createTime,
    this.updateTime,
    required this.avatar
  });

  SessionModel.fromJson(Map<String, dynamic> json):
        avatar = json['avatar'],
        createTime = DateTime.parse(json['createTime']),
        updateTime = null,
        creatorAccount = json['creatorAccount'],
        name = json['name'],
        id = json['id']
  ;

  Map<String, dynamic> toJson() => {
    "id": id,
    "name": name,
    "creatorAccount": creatorAccount,
    "createTime": createTime,
    "updateTime": updateTime,
    "avatar": avatar,
  };


  /// 查询用户会话列表
  static Future<List<SessionModel>> queryUserSessionList() async {
    var result = await dio.postResult("/store/session/list");
    var listData = result.data as List<dynamic>;
    return listData.map((e) => SessionModel.fromJson(e)).toList();
  }



}


/// 会话消息列表
class SessionMessageModel extends SessionModel {

  final ChatMessageModel? latestMessage;

  final int unReadCount;

  SessionMessageModel({
    required super.id,
    required super.name,
    required super.creatorAccount,
    required super.createTime,
    required super.avatar,
    required this.unReadCount,
    this.latestMessage,
  });

  SessionMessageModel.fromJson(Map<String, dynamic> jsonMap):
        unReadCount=jsonMap['unReadCount'] ?? 0,
        latestMessage=jsonMap['latestMessage']!=null
            ? ChatMessageModel.fromJson(jsonMap['latestMessage'])
            : null,
        super.fromJson(jsonMap)
  ;

}